package miiteen.customsportal.Api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javafx.beans.binding.ObjectExpression;
import miiteen.common.utils.JacksonMapper;
import miiteen.core.result.ResultMessage;
import miiteen.core.result.ResultMessageTool;
import miiteen.customsportal.basic.beans.BasicBrowsingHistory;
import miiteen.customsportal.basic.beans.BasicPageBrowsing;
import miiteen.customsportal.basic.service.BasicBrowsingHistoryService;
import miiteen.customsportal.basic.service.BasicPageBrowsingService;
import miiteen.data.beans.PageBounds;
import miiteen.demo.utils.CommonUtil;
import miiteen.demo.utils.IPUtils;
import miiteen.hr.pubconfig.CurrentHrInfo;
import miiteen.hr.user.beans.UserUserInfo;
import miiteen.sys.base.bean.BaseConditionVO;
import miiteen.sys.login.bean.UserShiro;
import miiteen.sys.login.utils.LoginUtil;
import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 浏览记录
 */
@Api(tags = "浏览记录")
@RestController
@RequestMapping("/Api/BasicBrowsingHistory")
@CrossOrigin
public class BasicBrowsingHistoryApiController {
    @Autowired
    private BasicBrowsingHistoryService basicBrowsingHistoryService;

    @Autowired
    private CommonUtil commonUtil;

    @Autowired
    private BasicPageBrowsingService basicPageBrowsingService;


    //新增浏览记录
    @ApiOperation("新增浏览记录")
    @RequestMapping(value = "/saveRecord", method = RequestMethod.POST)
    public ResultMessage<Object> saveRecord(@RequestBody BasicBrowsingHistory basicBrowsingHistory,HttpServletRequest request){
        basicBrowsingHistory.setScannerTime(new Date());
        basicBrowsingHistory.setIPAddress(IPUtils.findIp(request));
        basicBrowsingHistoryService.save(basicBrowsingHistory);
        BasicPageBrowsing basicPageBrowsing = basicPageBrowsingService.getTotalCount();
        int result=basicPageBrowsing.getTotalCount();
        if (result> 0) {
            return ResultMessageTool.Success("成功", result);
        } else {
            return ResultMessageTool.Error("失败");
        }
    }

    @ApiOperation("获取浏览IP地址")
    @RequestMapping(value = "/getIPAddress", method = RequestMethod.GET)
    public ResultMessage<Object> getIPAddress(HttpServletRequest httpServletRequest){

       String result = IPUtils.findIp(httpServletRequest);
        if (!"".equals(result)&&result!=null) {
            return ResultMessageTool.Success("成功", result);
        } else {
            return ResultMessageTool.Error("失败");
        }
    }
    //根据信息ID查总浏览数和当日浏览数（内部外部）
    @ApiOperation("根据信息ID查总浏览数和当日浏览数（内部外部）")
    @RequestMapping(value = "/getIdNumber", method = RequestMethod.GET)
    public ResultMessage<Object> getIdNumber(@ApiParam(name = "id", value = "浏览信息的ID（browseId 或者 interiorId 那个有值）") @RequestParam String id,@ApiParam(name = "ifMod", value = "tab的类型 内网=0，外网=1") @RequestParam String ifMod){

        if(id!=null&&!id.equals("")){
            Map<String, Object> map=new HashMap<>();
            map.put("id",id);
            map.put("ifMod",Integer.parseInt(ifMod));
            BasicBrowsingHistory byIdNumber = basicBrowsingHistoryService.getByIdNumber(map);
            if(byIdNumber==null){
                BasicBrowsingHistory basicBrowsingHistory=new BasicBrowsingHistory();
                basicBrowsingHistory.setAllNumber(0);
                basicBrowsingHistory.setDayNumber(0);
                return ResultMessageTool.Success("成功", basicBrowsingHistory);
            }
            return ResultMessageTool.Success("成功", byIdNumber);
        }else{
            return ResultMessageTool.Error("失败");
        }

    }


    @RequestMapping(value = "/historyList")
    public ModelAndView list2(BaseConditionVO vo) {
        //设置列表关联JSP页面
        ModelAndView mv = new ModelAndView("customsportal/basic/CustomsPortalHistoryList");
        //自行设置默认查询条件
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("list",0);
        if (!"".equals(vo.getArg1())) {
            map.put("scanner", vo.getArg1());
            commonUtil.handleLikeSearch(map,"scanner");
        }
        if (!"".equals(vo.getArg2())) {
            map.put("startTime", vo.getArg2());
        }
        if (!"".equals(vo.getArg3())) {
            map.put("endTime", vo.getArg3());
        }
        List<BasicBrowsingHistory> list=basicBrowsingHistoryService.findListByMap(map);
        mv.addObject("list", JacksonMapper.getListToJson(list));
        mv.addObject("vo", vo);
        return mv;
    }


    @ApiOperation("获取浏览记录")
    @RequestMapping(value = "/getHistoryList", method = RequestMethod.POST)
   public ResultMessage<Object> getList(@RequestBody  BaseConditionVO vo) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("list",0);
        if (!"".equals(vo.getArg1())) {
            map.put("scanner", vo.getArg1());
            commonUtil.handleLikeSearch(map,"scanner");
        }
        if (!"".equals(vo.getArg2())) {
            map.put("startTime", vo.getArg2());
        }
        if (!"".equals(vo.getArg3())) {
            map.put("endTime", vo.getArg3());
        }
        try {
            List<BasicBrowsingHistory> count = basicBrowsingHistoryService.findListByMap(map);
            PageBounds pageBounds = new PageBounds();
            if (vo.getPageNum() > 0 && vo.getPageSize() > 0) {
                int page = vo.getPageNum();
                int pageSize = vo.getPageSize();
                pageBounds.setPageSize(pageSize);
                pageBounds.setPageNum(page);
            }
            List<BasicBrowsingHistory> list = basicBrowsingHistoryService.findListByMap(map, pageBounds);
            map.clear();
            map.put("infoList", list);
            map.put("totalCount", count.size());
            return ResultMessageTool.Success("成功", map);
        }catch (Exception e){
            return ResultMessageTool.Error("查询出错");
        }
    }


}
